<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.5: http://docutils.sourceforge.net/" />
<title>autoconf指南</title>
<meta name="author" content="litaocheng" />
<meta name="date" content="2009.9.10" />
<style type="text/css">

/*
:Author: David Goodger (goodger@python.org)
:Id: $Id: html4css1.css 5196 2007-06-03 20:25:28Z wiemann $
:Copyright: This stylesheet has been placed in the public domain.

Default cascading style sheet for the HTML output of Docutils.

See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
customize this style sheet.
*/

/* used to remove borders from tables and images */
.borderless, table.borderless td, table.borderless th {
  border: 0 }

table.borderless td, table.borderless th {
  /* Override padding for "table.docutils td" with "! important".
     The right padding separates the table cells. */
  padding: 0 0.5em 0 0 ! important }

.first {
  /* Override more specific margin styles with "! important". */
  margin-top: 0 ! important }

.last, .with-subtitle {
  margin-bottom: 0 ! important }

.hidden {
  display: none }

a.toc-backref {
  text-decoration: none ;
  color: black }

blockquote.epigraph {
  margin: 2em 5em ; }

dl.docutils dd {
  margin-bottom: 0.5em }

/* Uncomment (and remove this text!) to get bold-faced definition list terms
dl.docutils dt {
  font-weight: bold }
*/

div.abstract {
  margin: 2em 5em }

div.abstract p.topic-title {
  font-weight: bold ;
  text-align: center }

div.admonition, div.attention, div.caution, div.danger, div.error,
div.hint, div.important, div.note, div.tip, div.warning {
  margin: 2em ;
  border: medium outset ;
  padding: 1em }

div.admonition p.admonition-title, div.hint p.admonition-title,
div.important p.admonition-title, div.note p.admonition-title,
div.tip p.admonition-title {
  font-weight: bold ;
  font-family: sans-serif }

div.attention p.admonition-title, div.caution p.admonition-title,
div.danger p.admonition-title, div.error p.admonition-title,
div.warning p.admonition-title {
  color: red ;
  font-weight: bold ;
  font-family: sans-serif }

/* Uncomment (and remove this text!) to get reduced vertical space in
   compound paragraphs.
div.compound .compound-first, div.compound .compound-middle {
  margin-bottom: 0.5em }

div.compound .compound-last, div.compound .compound-middle {
  margin-top: 0.5em }
*/

div.dedication {
  margin: 2em 5em ;
  text-align: center ;
  font-style: italic }

div.dedication p.topic-title {
  font-weight: bold ;
  font-style: normal }

div.figure {
  margin-left: 2em ;
  margin-right: 2em }

div.footer, div.header {
  clear: both;
  font-size: smaller }

div.line-block {
  display: block ;
  margin-top: 1em ;
  margin-bottom: 1em }

div.line-block div.line-block {
  margin-top: 0 ;
  margin-bottom: 0 ;
  margin-left: 1.5em }

div.sidebar {
  margin: 0 0 0.5em 1em ;
  border: medium outset ;
  padding: 1em ;
  background-color: #ffffee ;
  width: 40% ;
  float: right ;
  clear: right }

div.sidebar p.rubric {
  font-family: sans-serif ;
  font-size: medium }

div.system-messages {
  margin: 5em }

div.system-messages h1 {
  color: red }

div.system-message {
  border: medium outset ;
  padding: 1em }

div.system-message p.system-message-title {
  color: red ;
  font-weight: bold }

div.topic {
  margin: 2em }

h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
  margin-top: 0.4em }

h1.title {
  text-align: center }

h2.subtitle {
  text-align: center }

hr.docutils {
  width: 75% }

img.align-left {
  clear: left }

img.align-right {
  clear: right }

ol.simple, ul.simple {
  margin-bottom: 1em }

ol.arabic {
  list-style: decimal }

ol.loweralpha {
  list-style: lower-alpha }

ol.upperalpha {
  list-style: upper-alpha }

ol.lowerroman {
  list-style: lower-roman }

ol.upperroman {
  list-style: upper-roman }

p.attribution {
  text-align: right ;
  margin-left: 50% }

p.caption {
  font-style: italic }

p.credits {
  font-style: italic ;
  font-size: smaller }

p.label {
  white-space: nowrap }

p.rubric {
  font-weight: bold ;
  font-size: larger ;
  color: maroon ;
  text-align: center }

p.sidebar-title {
  font-family: sans-serif ;
  font-weight: bold ;
  font-size: larger }

p.sidebar-subtitle {
  font-family: sans-serif ;
  font-weight: bold }

p.topic-title {
  font-weight: bold }

pre.address {
  margin-bottom: 0 ;
  margin-top: 0 ;
  font-family: serif ;
  font-size: 100% }

pre.literal-block, pre.doctest-block {
  margin-left: 2em ;
  margin-right: 2em }

span.classifier {
  font-family: sans-serif ;
  font-style: oblique }

span.classifier-delimiter {
  font-family: sans-serif ;
  font-weight: bold }

span.interpreted {
  font-family: sans-serif }

span.option {
  white-space: nowrap }

span.pre {
  white-space: pre }

span.problematic {
  color: red }

span.section-subtitle {
  /* font-size relative to parent (h1..h6 element) */
  font-size: 80% }

table.citation {
  border-left: solid 1px gray;
  margin-left: 1px }

table.docinfo {
  margin: 2em 4em }

table.docutils {
  margin-top: 0.5em ;
  margin-bottom: 0.5em }

table.footnote {
  border-left: solid 1px black;
  margin-left: 1px }

table.docutils td, table.docutils th,
table.docinfo td, table.docinfo th {
  padding-left: 0.5em ;
  padding-right: 0.5em ;
  vertical-align: top }

table.docutils th.field-name, table.docinfo th.docinfo-name {
  font-weight: bold ;
  text-align: left ;
  white-space: nowrap ;
  padding-left: 0 }

h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
  font-size: 100% }

ul.auto-toc {
  list-style-type: none }

</style>
</head>
<body>
<div class="document" id="autoconf">
<h1 class="title">autoconf指南</h1>
<table class="docinfo" frame="void" rules="none">
<col class="docinfo-name" />
<col class="docinfo-content" />
<tbody valign="top">
<tr><th class="docinfo-name">Author:</th>
<td>litaocheng</td></tr>
<tr class="field"><th class="docinfo-name">Mail:</th><td class="field-body"><a class="reference external" href="mailto:litaocheng&#64;gmail.com">litaocheng&#64;gmail.com</a></td>
</tr>
<tr><th class="docinfo-name">Date:</th>
<td>2009.9.10</td></tr>
</tbody>
</table>
<div class="contents topic" id="id1">
<p class="topic-title first">目录</p>
<ul class="auto-toc simple">
<li><a class="reference internal" href="#id2" id="id3">1&nbsp;&nbsp;&nbsp;概述</a></li>
<li><a class="reference internal" href="#hello-world-version1" id="id4">2&nbsp;&nbsp;&nbsp;hello world (version1)</a></li>
<li><a class="reference internal" href="#hello-world-version-2" id="id5">3&nbsp;&nbsp;&nbsp;hello world (version 2)</a></li>
</ul>
</div>
<div class="section" id="id2">
<h1><a class="toc-backref" href="#id3">1&nbsp;&nbsp;&nbsp;概述</a></h1>
<p>在linux下工作，不可避免的要接触makefile.
我们经常调用 <tt class="docutils literal"><span class="pre">./configure</span> <span class="pre">&amp;&amp;</span> <span class="pre">make</span> <span class="pre">&amp;&amp;</span> <span class="pre">make</span> <span class="pre">install</span></tt> 安装某个应用.</p>
<p>让我们先看一章autoconf系列的流程图：</p>
<img alt="img/autoconf.png" src="img/autoconf.png" />
</div>
<div class="section" id="hello-world-version1">
<h1><a class="toc-backref" href="#id4">2&nbsp;&nbsp;&nbsp;hello world (version1)</a></h1>
<p>在这个版本中，我们只有一个 <strong>hello.c</strong> 源文件，其仅仅打印 &quot;hello world!&quot;.</p>
<p>使用你喜欢的编辑器书写 hello.c</p>
<pre class="literal-block">
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;

int main(int argc, char * argv[])
{
   printf(&quot;hello world!\n&quot;);
   return 0;
}
</pre>
<p>我们的代码就编辑完成了，接下来，我们就开始autoconf之旅.</p>
<p>首先说一下简单的步骤:</p>
<ol class="arabic simple">
<li>autoscan 生成一个configure的模板</li>
<li>aclocal</li>
<li>autoconf</li>
<li>automake</li>
<li>./configure &amp;&amp; make &amp;&amp; make install</li>
</ol>
<p>首先在hello1中，执行:</p>
<pre class="literal-block">
autoscan
</pre>
<p>其为我们生成了 <strong>configure.scan</strong> 文件，我们需要修改这个文件为 <strong>configure.ac</strong></p>
<pre class="literal-block">
$ mv configure.scan configure.ac
</pre>
<p>进行一些编辑:</p>
<pre class="literal-block">
$ vim configure.ac

# 修改AC_INIT
AC_INIT([helloworld], [0.1], [litaocheng&#64;gmail.com])

# 添加AM_INIT_AUTOMAKE
AM_INIT_AUTOMAKE(helloworld, 0.1)

# 修改AC_OUTPUT
AC_OUTPUT(Makefile)
</pre>
<p>configure.ac修改完成后，调用</p>
<pre class="literal-block">
$ aclocal
$ autoconf
$ autoheader
</pre>
<p>至此 <strong>configure</strong> 已经生成，但是还没完，我们需要编写一个 <strong>Makefile.am</strong> 文件，automke 利用其生成 <strong>Makefile.in</strong></p>
<pre class="literal-block">
$ vim Makefile.am
UTOMAKE_OPTIONS=foreign
bin_PROGRAMS=helloworld
helloworld_SOURCES=hello.c
</pre>
<p>Makefile.am比较简单， <tt class="docutils literal"><span class="pre">bin_PROGRAMS=helloworld</span></tt> 表明我们要最终生成的可执行程序, <tt class="docutils literal"><span class="pre">helloworld_SOURCES=hello.c</span></tt> 表明对应
的源文件，这里，我们只有一个 <tt class="docutils literal"><span class="pre">hello.c</span></tt>.</p>
<p>接下来，我们要生成 <tt class="docutils literal"><span class="pre">Makefile.in</span></tt></p>
<pre class="literal-block">
$ automake --add-missing
</pre>
<p>加上add-missing 是因为依照GNU的代码规范，我们缺少了很多相关的内容，我们让automake
自动为我们添加.</p>
<p>至此我们所有的准备工作已经完成，我们可以运行那么&quot;熟悉&quot;的过程</p>
<pre class="literal-block">
$ ./continue
$ make
$ sudo make install
</pre>
<p>好了，一切完成后，运行</p>
<pre class="literal-block">
$ helloworld
hello world!
</pre>
<p>大功告成!</p>
<p>让我们对hello.c进行轻微的修改</p>
<pre class="literal-block">
int main(int argc, char * argv[])
{
+     printf(&quot;hello world! (vsn 1)\n&quot;);
-     printf(&quot;hello world!\n&quot;);
      return 0;
}
</pre>
<p>然后运行</p>
<pre class="literal-block">
$ ./configure &amp;&amp; make
$ ./helloworld
hello world! (vsn 1)
</pre>
<p>当我们发布代码的时候，只要包含 <strong>configure</strong>, <strong>Makefile.in</strong> 即可.</p>
<p>让我们书写一个小脚本 <strong>autogen.sh</strong> ，可以用来生成configure</p>
<pre class="literal-block">
#! /bin/bash
aclocal &amp;&amp; \
autoheader &amp;&amp; \
autoconf &amp;&amp; \
automake --add-missing --copy
</pre>
<p>这个小脚本根据 <strong>configure.ac</strong>, <strong>config.h.in</strong>, <strong>Makefile.am</strong> 生成configure, Makefile.</p>
<p>接下来运行</p>
<pre class="literal-block">
$ ./autogen.sh
</pre>
<p>就会生成configure, Makefile</p>
</div>
<div class="section" id="hello-world-version-2">
<h1><a class="toc-backref" href="#id5">3&nbsp;&nbsp;&nbsp;hello world (version 2)</a></h1>
<p>在这个版本中，会将代码组织的更加合理，会加入更多的代码.</p>
<p>目录结构</p>
<pre class="literal-block">
hello2
  |-- src/
  |-- doc/
  |-- include/
  |-- test/
</pre>
</div>
</div>
</body>
</html>
